package main

import (
	"fmt"
	"sort"
)

func main() {
	fmt.Print("Map test!!")
	test1()
	test2()
	sortedMap()
	invertMap()
}

func invertMap() {
	barVal := map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
		"delta": 87, "echo": 56, "foxtrot": 12,
		"golf": 34, "hotel": 16, "indio": 87,
		"juliet": 65, "kili": 43, "lima": 98}
	fmt.Println("UnInvert:")
	for k, v := range barVal{
		fmt.Printf("Key: %v, Value: %v \n ", k, v)
	}

	invMap:=make(map[int]string, len(barVal))
	for k, v:=range barVal{
		invMap[v] = k
	}
	fmt.Println("Invert:")
	for k, v := range invMap{
		fmt.Printf("Key: %v, Value: %v \n ", k, v)
	}

}

func sortedMap() {
	barVal := map[string]int{"alpha": 34, "bravo": 56, "charlie": 23,
		"delta": 87, "echo": 56, "foxtrot": 12,
		"golf": 34, "hotel": 16, "indio": 87,
		"juliet": 65, "kili": 43, "lima": 98}

	fmt.Println("unsorted:")
	for k, v := range barVal{
		fmt.Printf("Key: %v, Value: %v \n ", k, v)
	}

	keys := make([]string, len(barVal))
	i := 0
	for k, _ := range barVal{
		keys[i] = k
		i++
	}
	sort.Strings(keys)
	fmt.Println()
	fmt.Println("Sorted: ")
	for _, k := range keys{
		fmt.Printf("key: %v, Value: %v \n", k, barVal[k])
	}
}

func test2() {
	items:=make([]map[int]int, 5)
	for i:=range items{
		items[i] = make(map[int]int, 1)
		items[i][1] = 2
	}

	fmt.Printf("Version A: Value of items: %v\n", items)

	//Version B: NOT GOOD
	imte2 := make([]map[int]int, 5)
	for _, item := range imte2{
		item = make(map[int]int, 1)
		item[1] = 2
	}

	fmt.Printf("Version B: Value of items: %v\n", imte2)
}

func test1() {
	var mapList map[string]int
	var mapAssigned map[string]int

	mapList = map[string]int{"one":1, "two":2}
	mapCreated := make(map[string]float32)
	mapAssigned = mapList

	mapCreated["key1"] = 4.5
	mapCreated["key2"] = 3.14159
	mapAssigned["two"] = 3

	fmt.Printf("Map literal at \"one\" is: %d\n", mapList["one"])
	fmt.Printf("Map created at \"key2\" is: %f\n", mapCreated["key2"])
	fmt.Printf("Map assigned at \"two\" is: %d\n", mapList["two"])
	fmt.Printf("Map literal at \"ten\" is: %d\n", mapList["ten"])
}
